from fastapi import APIRouter, HTTPException

from app.schema import ResponseSchema
from app.service.province import ProvinceService
from app.service.news import NewsService
from app.service.school_service import SchoolInfoService

router = APIRouter(prefix="/data", tags=['data'])


@router.get("/province", response_model=ResponseSchema, response_model_exclude_none=True)
async def get_province():
    result = await ProvinceService.get_all_provinces()
    return ResponseSchema(detail="success", result=result)


@router.get("/news", response_model=ResponseSchema, response_model_exclude_none=True)
async def get_news():
    result = await NewsService.get_all_news()
    return ResponseSchema(detail="success", result=result)

@router.get("/news/{news_id}", response_model=ResponseSchema, response_model_exclude_none=True)
async def get_news_by_id(news_id: int):
    result = await NewsService.get_news_by_id(news_id)
    if result:
        return ResponseSchema(detail="success", result=result)
    else:
        raise HTTPException(status_code=404, detail="News item not found !")

@router.post("/update_news", response_model=ResponseSchema)
async def update_news():
    result1, result2 = await NewsService.update_news()
    if (result2 != 0):
        return ResponseSchema(detail="success", result=f'{result2} items found, {result1} items added');
    else:
        raise HTTPException(status_code=400, detail="Update Failed !")


